﻿@{
    ViewData["Title"] = "组织列表";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Name" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Code" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn" lay-submit lay-filter="btnsearch">
                            <i class="layui-icon layui-icon-search"></i> 查询
                        </button>
                    </div>
                </div>
            </form>

            <table id="lsttable" lay-filter="lsttable"></table>
            <script type="text/html" id="toolbar">
                <button class="layui-btn layui-btn-sm" lay-event="add">
                    <i class="layui-icon layui-icon-addition"></i> 添加
                </button>
            </script>
            <script type="text/html" id="bar">
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

@section scripts {
    <script type="text/javascript">
        var app = {};
        layui.use(['tools', 'toast', 'http'], function () {
            var treeTable = layui.treeTable,
                $ = layui.$,
                form = layui.form,
                http = layui.http,
                toast = layui.toast,
                tools = layui.tools;
            //
            app.add = function (data) {
                tools.open({
                    area: ['700px', '100%'],
                    content: './add',
                    title: '用户管理',
                    success: function (layero, index, that) {
                        tools.initValue(layero, data);
                    },
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.app.save();
                    }
                });
            }
            app.del = function (id) {
                var index = layer.confirm('您确定要删除当前组织吗？', function () {
                    http.post('/api/sysOrg/delete', { id }).then((res) => {
                        if (res.code == 200) {
                            layer.close(index);
                            treeTable.reload('lsttable');
                            toast.success({ message: '删除成功！' });
                        }
                        else {
                            layer.alert('删除失败，错误信息:' + res.message, { icon: 2 });
                        }
                    })
                });
            }
            //
            app.init = function () {
                //
                form.on('submit(btnsearch)', function (data) {
                    treeTable.reload('lsttable', {
                        where: data.field
                    })
                    return false;
                });
                //
                let cols =
                    [[
                        { title: 'id', field: 'id', width: 160, fixed: 'left' },
                        { title: '名称', field: 'name', fixed: 'left' },
                        { title: '排序', field: 'orderNo', width: 100 },
                        { title: '代码', field: 'code', width: 120 },
                        { fixed: 'right', title: '操作', toolbar: '#bar', align: 'right', width: 120 }
                    ]];
                //
                tools.TreeTable({ url: '/api/sysOrg/list?Id=0', cols: cols });
                treeTable.on('toolbar(lsttable)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            app.add();
                            break;
                    };
                });
                treeTable.on('tool(lsttable)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'edit':
                            app.add(data);
                            break;
                        case 'del':
                            app.del(data.id);
                            break;
                    };
                });
            }();
        })
    </script>
}
